package com.cn.ZhiKeDianShang.ui.activity;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.os.Bundle;

import android.os.Environment;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.ZhiKeDianShang.AppConfig;
import com.cn.ZhiKeDianShang.Oss.OssService;
import com.cn.ZhiKeDianShang.R;
import com.cn.ZhiKeDianShang.base.BaseActivity;
import com.cn.ZhiKeDianShang.model.response.UserInfoBean;
import com.cn.ZhiKeDianShang.model.response.getUrlBean;
import com.cn.ZhiKeDianShang.model.response.zhaoYaojingBean;
import com.cn.ZhiKeDianShang.presenter.LoginUserPresenter;
import com.cn.ZhiKeDianShang.presenter.view.LoginUserView;
import com.cn.ZhiKeDianShang.util.BitmapUtil;
import com.cn.ZhiKeDianShang.util.ClipboardUtils;
import com.cn.ZhiKeDianShang.util.Netdialog.StyledDialog;
import com.cn.ZhiKeDianShang.util.PrefShare;
import com.cn.ZhiKeDianShang.util.ToastUtil;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.HttpUtils;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.result.ObjectResult;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.result.Result;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.compress.FileUtil;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import id.zelory.compressor.Compressor;
import io.reactivex.functions.Consumer;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/7/3.
 * do what to 2017/7/3
 */

public class zyjActivity extends BaseActivity<LoginUserView, LoginUserPresenter> implements LoginUserView {

    @BindView(R.id.setting_back)
    ImageView settingBack;
    @BindView(R.id.et_text)
    EditText etText;
    @BindView(R.id.im_sousuo)
    ImageView imSousuo;
    @BindView(R.id.tv_photo_identify)
    TextView tvPhotoIdentify;
    @BindView(R.id.tv_serch)
    TextView tv_serch;
    @BindView(R.id.tv_buyname)
    TextView tvBuyname;
    @BindView(R.id.tv_nameconform)
    TextView tvNameconform;
    @BindView(R.id.tv_buyerCre)
    TextView tvBuyerCre;
    @BindView(R.id.tv_created)
    TextView tvCreated;
    @BindView(R.id.tv_sellerCredit)
    TextView tvSellerCredit;
    @BindView(R.id.tv_registDay)
    TextView tvRegistDay;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.tv_buyerAvg)
    TextView tvBuyerAvg;
    @BindView(R.id.tv_received_rate)
    TextView tvReceivedRate;
    @BindView(R.id.tv_sent_rate)
    TextView tvSentRate;
    @BindView(R.id.tv_queryTime)
    TextView tvQueryTime;
    @BindView(R.id.ll_info1)
    LinearLayout llInfo1;
    @BindView(R.id.tv_type1)
    TextView tvType1;
    @BindView(R.id.tv_type2)
    TextView tvType2;
    @BindView(R.id.tv_type3)
    TextView tvType3;
    @BindView(R.id.tv_type4)
    TextView tvType4;
    @BindView(R.id.tv_type5)
    TextView tvType5;
    @BindView(R.id.tv_type6)
    TextView tvType6;
    @BindView(R.id.tv_downNum)
    TextView tvDownNum;
    @BindView(R.id.tv_goodNum)
    TextView tvGoodNum;
    @BindView(R.id.tv_midNum)
    TextView tvMidNum;
    @BindView(R.id.tv_badNum)
    TextView tvBadNum;
    @BindView(R.id.tv_nearWeekShop)
    TextView tvNearWeekShop;
    @BindView(R.id.tv_lastWeekShop)
    TextView tvLastWeekShop;
    @BindView(R.id.ll_info2)
    LinearLayout llInfo2;

    @Override
    protected int getNormalLayoutId() {
        return R.layout.activity_zhaoyaojing_info;
    }

    @Override
    protected int getErrorLayoutId() {
        return R.layout.activity_zhaoyaojing_info;
    }

    @Override
    protected LoginUserPresenter createPresenter() {
        return new LoginUserPresenter(this, this);
    }

    @Override
    protected void initViewsAndEvents() {
    }


    /**
     * 根据图片识别卖家id
     */
    private void identify(String url) {
//        DialogHelper
        Map<String, String> params = new HashMap<>();
        params.put("url", url);          // 账号登陆的时候需要MD5加密，服务器需求
        HttpUtils.post().url(AppConfig.COMMON_IDENTIFY)
                .params(params)
                .addSecret()
                .build()
                .execute(new BaseCallback<getUrlBean>(getUrlBean.class) {
                    @Override
                    public void onResponse(ObjectResult<getUrlBean> result) {
//                        DialogHelper.dismissProgressDialog();
                        Log.d("Debug", "到达请求这里---");
                        if (Result.checkSuccess(zyjActivity.this, result)) {
                            getUrlBean data = result.getData();
                            ToastUtil.showShort(result.getResultMsg());
                            etText.setText(data.getAccount() + "");
                        }
                        StyledDialog.dismiss(gloablDialog);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
//                        DialogHelper.dismissProgressDialog();
//                        ToastUtil.showErrorNet(LoginActivity.this);
                        StyledDialog.dismiss(gloablDialog);
                    }
                });
    }


    /**
     * 根据扫描出的二维码  进行识别
     */
    private void codeidentify(String url) {
//        DialogHelper
        Map<String, String> params = new HashMap<>();
        params.put("url", url);          // 账号登陆的时候需要MD5加密，服务器需求
        HttpUtils.post().url(AppConfig.COMMON_SAO)
                .params(params)
                .addSecret()
                .build()
                .execute(new BaseCallback<getUrlBean>(getUrlBean.class) {
                    @Override
                    public void onResponse(ObjectResult<getUrlBean> result) {
//                        DialogHelper.dismissProgressDialog();
                        Log.d("Debug", "到达请求这里---");
                        if (Result.checkSuccess(zyjActivity.this, result)) {
                            getUrlBean data = result.getData();
                            ToastUtil.showShort(result.getResultMsg());
                            etText.setText(data.getAccount() + "");
                        }
                        StyledDialog.dismiss(gloablDialog);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
//                        DialogHelper.dismissProgressDialog();
//                        ToastUtil.showErrorNet(LoginActivity.this);
                        StyledDialog.dismiss(gloablDialog);
                    }
                });
    }


    /**
     * 电商账号查看
     */
    private void ecaSerach(String account) {
//        DialogHelper
        Map<String, String> params = new HashMap<>();
        params.put("account", account);          // 账号登陆的时候需要MD5加密，服务器需求
        HttpUtils.post().url(AppConfig.ECA_SERCH)
                .params(params)
                .addSecret()
                .build()
                .execute(new BaseCallback<zhaoYaojingBean>(zhaoYaojingBean.class) {
                    @Override
                    public void onResponse(ObjectResult<zhaoYaojingBean> result) {
//                        DialogHelper.dismissProgressDialog();
                        try {
                            tv_serch.setEnabled(true);
                            tv_serch.setBackground(getResources().getDrawable(R.drawable.button_shape_blue_5));
                            Log.d("Debug", "到达请求这里---");
                            if (result.getResultCode() == 1) {
                                llInfo1.setVisibility(View.VISIBLE);
                                llInfo2.setVisibility(View.VISIBLE);
                                initview(result.getData().getResult());
                            } else {
                                llInfo1.setVisibility(View.GONE);
                                llInfo2.setVisibility(View.GONE);
                            }
                            Toast.makeText(zyjActivity.this, result.getResultMsg(), Toast.LENGTH_LONG).show();
                        } catch (Exception e) {

                        }
//                        ToastUtil.showLong(result.getResultMsg());
                    }

                    @Override
                    public void onError(Call call, Exception e) {
//                        DialogHelper.dismissProgressDialog();
//                        ToastUtil.showErrorNet(LoginActivity.this);
                        llInfo1.setVisibility(View.GONE);
                        llInfo2.setVisibility(View.GONE);
                    }
                });


    }


    /**
     * 给界面赋值
     */
    public void initview(zhaoYaojingBean.ResultBean ResultBean) {
        tvBuyname.setText(etText.getText().toString());

        String NameconformColor = ResultBean.getNameconform_word_color();
        String NameconformText = ResultBean.getNameconform_word();
//        if (ResultBean.getNameconform() == 1) {
//            NameconformText = "已实名认证";
//            NameconformColor = "#17ad03";
//        } else {
//            NameconformText = "未实名认证";
//            NameconformColor = "#fb2626";
//        }

        String vip_levelColor = "";
        String vip_levelText = "";
        if (ResultBean.getVip_level().equals("0")) {
            vip_levelText = "普通会员";
            vip_levelColor = "#ff7c00";
        } else {
            vip_levelText = "超级会员";
            vip_levelColor = "#ddc18d";
        }

        String vip_infoColor = "";
        String vip_infoText = "";

        if (ResultBean.getVip_info().equals("c") || ResultBean.getVip_info().equals("asso_vip")) {
            vip_infoText = "";
            vip_infoColor = "#dbb26f";
        } else if (ResultBean.getVip_info().equals("vip1")) {
            vip_infoText = "淘宝V1会员";
            vip_infoColor = "#dbb26f";
        } else if (ResultBean.getVip_info().equals("vip2")) {
            vip_infoText = "淘宝V2会员";
            vip_infoColor = "#fcb100";
        } else if (ResultBean.getVip_info().equals("vip3")) {
            vip_infoText = "淘宝V3会员";
            vip_infoColor = "#fa7c02";
        } else if (ResultBean.getVip_info().equals("vip4")) {
            vip_infoText = "淘宝V4会员";
            vip_infoColor = "#e32c17";
        } else if (ResultBean.getVip_info().equals("vip5")) {
            vip_infoText = "淘宝V5会员";
            vip_infoColor = "#be20ee";
        } else if (ResultBean.getVip_info().equals("vip6")) {
            vip_infoText = "淘宝V6会员";
            vip_infoColor = "#613f71";
        }

        //首先是拼接字符串
        String content1 = "<font color= " + NameconformColor + ">" + NameconformText + "</font>";
        String content2 = "<font color= " + vip_levelColor + ">" + vip_levelText + "</font>";
        String content3 = "<font color= " + vip_infoColor + ">" + vip_infoText + "</font>";
//然后直接setText()
        tvNameconform.setText(Html.fromHtml("实名认证：" + content1 + content2 + content3));

//        //1实名认证 其他状态没有
//        if (ResultBean.getNameconform() == 1) {
//            tvNameconform.setText("已实名认证");
//            tvNameconform.setTextColor(getResources().getColor(R.color.text_color_pass));
//        } else {
//            tvNameconform.setText("未实名认证");
//            tvNameconform.setTextColor(getResources().getColor(R.color.text_color_error));
//        }

        //买家信誉
        if (ResultBean.getBuyerCre().equals("0心")) {
            tvBuyerCre.setTextColor(getResources().getColor(R.color.text_color_error));
        } else {
            tvBuyerCre.setTextColor(getResources().getColor(R.color.text_color_little_black));
        }
        tvBuyerCre.setText(ResultBean.getBuyerCre());
        //注册时间
        if (ResultBean.getCreated().equals("无数据")) {
            tvCreated.setTextColor(getResources().getColor(R.color.text_color_error));
            tvCreated.setText("无数据");
        } else {
            tvCreated.setTextColor(getResources().getColor(R.color.text_color_little_black));
            tvCreated.setText(ResultBean.getCreated());

        }

        //商家信誉
        if (ResultBean.getSellerCredit().equals("未开店")) {
            tvSellerCredit.setTextColor(getResources().getColor(R.color.text_color_error));
        } else {
            tvSellerCredit.setTextColor(getResources().getColor(R.color.text_color_little_black));
        }
        tvSellerCredit.setText(ResultBean.getSellerCredit());


        //淘龄
        if (ResultBean.getRegistDay().equals("无数据")) {
            tvRegistDay.setTextColor(getResources().getColor(R.color.text_color_error));
            tvRegistDay.setText("无数据");

        } else {
            tvRegistDay.setTextColor(getResources().getColor(R.color.text_color_little_black));
            tvRegistDay.setText(ResultBean.getRegistDay());
        }


        //性别
        if (ResultBean.getSex().equals("保密")) {
            tvSex.setTextColor(getResources().getColor(R.color.text_color_error));
        } else {
            tvSex.setTextColor(getResources().getColor(R.color.text_color_little_black));
        }
        tvSex.setText(ResultBean.getSex());

//       卖家周平均
        tvBuyerAvg.setText(ResultBean.getBuyerAvg() + "");

//        收到好评
        if (ResultBean.getReceived_rate().equals("无数据")) {
            tvReceivedRate.setTextColor(getResources().getColor(R.color.text_color_error));
            tvReceivedRate.setText("无数据");
        } else {
            tvReceivedRate.setTextColor(getResources().getColor(R.color.text_color_little_black));
            tvReceivedRate.setText(ResultBean.getReceived_rate());
        }


        //   发出好评
        if (ResultBean.getSent_rate().equals("无数据") || TextUtils.isEmpty(ResultBean.getSent_rate())) {
            tvSentRate.setTextColor(getResources().getColor(R.color.text_color_error));
            tvSentRate.setText("无数据");
        } else {
            tvSentRate.setTextColor(getResources().getColor(R.color.text_color_little_black));
            tvSentRate.setText(ResultBean.getSent_rate());
        }


        //查询时间
        tvQueryTime.setText(ResultBean.getQueryTime() + "");


//        type1
        if (ResultBean.getType1() > 0) {
            tvType1.setText(ResultBean.getType1() + "");
            tvType1.setTextColor(getResources().getColor(R.color.text_color_error));
            tvType1.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        } else {
            tvType1.setText(0 + "");
            tvType1.setTextColor(getResources().getColor(R.color.cut_line_color));
            tvType1.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }


        //        type2
        if (ResultBean.getType2() > 0) {
            tvType2.setText(ResultBean.getType2() + "");
            tvType2.setTextColor(getResources().getColor(R.color.text_color_error));
            tvType2.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        } else {
            tvType2.setText(0 + "");
            tvType2.setTextColor(getResources().getColor(R.color.cut_line_color));
            tvType2.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }


        //        type3
        if (ResultBean.getType3() > 0) {
            tvType3.setText(ResultBean.getType3() + "");
            tvType3.setTextColor(getResources().getColor(R.color.text_color_error));
            tvType3.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        } else {
            tvType3.setText(0 + "");
            tvType3.setTextColor(getResources().getColor(R.color.cut_line_color));
            tvType3.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }

        //        type4
        if (ResultBean.getType4() > 0) {
            tvType4.setText(ResultBean.getType4() + "");
            tvType4.setTextColor(getResources().getColor(R.color.text_color_error));
            tvType4.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        } else {
            tvType4.setText(0 + "");
            tvType4.setTextColor(getResources().getColor(R.color.cut_line_color));
            tvType4.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }

        //        type5
        if (ResultBean.getType5() > 0) {
            tvType5.setText(ResultBean.getType5() + "");
            tvType5.setTextColor(getResources().getColor(R.color.text_color_error));
            tvType5.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        } else {
            tvType5.setText(0 + "");
            tvType5.setTextColor(getResources().getColor(R.color.cut_line_color));
            tvType5.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }


        //        type6
        if (ResultBean.getType6() > 0) {
            tvType6.setText(ResultBean.getType6() + "");
            tvType6.setTextColor(getResources().getColor(R.color.text_color_error));
            tvType6.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        } else {
            tvType6.setText(0 + "");
            tvType6.setTextColor(getResources().getColor(R.color.cut_line_color));
            tvType6.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }


        //降权

        if (ResultBean.getDownNum() > 0) {
            tvDownNum.setText(ResultBean.getDownNum() + "");
            tvDownNum.setTextColor(getResources().getColor(R.color.text_color_error));
            tvDownNum.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        } else {
            tvDownNum.setText(0 + "");
            tvDownNum.setTextColor(getResources().getColor(R.color.cut_line_color));
            tvDownNum.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }


        //好评goodNum
        if (ResultBean.getGoodNum() > 0) {
            tvGoodNum.setText(ResultBean.getGoodNum() + "");
            tvGoodNum.setTextColor(getResources().getColor(R.color.text_color_pass));
            tvGoodNum.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        } else {
            tvGoodNum.setText(0 + "");
            tvGoodNum.setTextColor(getResources().getColor(R.color.cut_line_color));
            tvGoodNum.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }

        //差评 goodNum
        if (ResultBean.getBadNum() > 0) {
            tvBadNum.setText(ResultBean.getBadNum() + "");
            tvBadNum.setTextColor(getResources().getColor(R.color.text_color_error));
            tvBadNum.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        } else {
            tvBadNum.setText(0 + "");
            tvBadNum.setTextColor(getResources().getColor(R.color.cut_line_color));
            tvBadNum.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }

        //证明
        if (ResultBean.getProveNum() > 0) {
            tvMidNum.setText(ResultBean.getProveNum() + "");
            tvMidNum.setTextColor(getResources().getColor(R.color.text_color_pass));
            tvMidNum.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        } else {
            tvMidNum.setText(0 + "");
            tvMidNum.setTextColor(getResources().getColor(R.color.cut_line_color));
            tvMidNum.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }


        tvNearWeekShop.setText(ResultBean.getNearWeekShop() + "");

        tvLastWeekShop.setText(ResultBean.getLastWeekShop() + "");


    }

    @Override
    public void onLogin(UserInfoBean resultBean) {

    }

    @Override
    public void requestFailed(String message) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.setting_back, R.id.im_sousuo, R.id.tv_photo_identify, R.id.tv_serch})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.setting_back:
                finish();
                break;
            case R.id.tv_photo_identify:
                initPermission();
                break;
            case R.id.im_sousuo:
            case R.id.tv_serch:
                if (etText.getText().toString().length() == 0) {
                    ToastUtil.showShort("请输入买家淘宝账号");
                    return;
                }
                tv_serch.setEnabled(false);
                tv_serch.setBackground(getResources().getDrawable(R.drawable.button_shape_cccccc_5));
                ClipboardUtils.setClipboard(zyjActivity.this, etText.getText().toString(), "买家淘宝账号复制成功");
                ecaSerach(etText.getText().toString());
                break;
        }
    }


    private void initPermission() {
        RxPermissions rxPermissions = new RxPermissions(this);

        rxPermissions.request(
                Manifest.permission.CAMERA
                , Manifest.permission.WRITE_EXTERNAL_STORAGE
                , Manifest.permission.READ_EXTERNAL_STORAGE
        ).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean) {
                    Intent  intent= new Intent(zyjActivity.this,TestScanActivity.class);
                    startActivityForResult(intent, 888);

////                    申请的权限全部允许
//                    Intent intent = new Intent(zyjActivity.this, ScannerActivity.class);
//                    // 设置扫码框的宽
//                    intent.putExtra(Constant.EXTRA_SCANNER_FRAME_WIDTH, DpPxUtils.dip2px(zyjActivity.this, 250));
//                    // 设置扫码框的高
//                    intent.putExtra(Constant.EXTRA_SCANNER_FRAME_HEIGHT, DpPxUtils.dip2px(zyjActivity.this, 250));
//                    // 设置扫码框距顶部的位置
//                    intent.putExtra(Constant.EXTRA_SCANNER_FRAME_TOP_PADDING, DpPxUtils.dip2px(zyjActivity.this, 100));
//                    // 可以从相册获取
//                    intent.putExtra(Constant.EXTRA_IS_ENABLE_SCAN_FROM_PIC, true);
//                    startActivityForResult(intent, 888);
//
//                    //下面是拍照操作
//                    Matisse.from(zyjActivity.this)
//                            .choose(MimeType.ofImage(), false)
//                            .showSingleMediaType(true)
//                            .countable(true)
//                            .capture(true)
//                            .isCrop(false)
//                            .captureStrategy(
//                                    new CaptureStrategy(true, "com.cn.ZhiKeDianShang"))
//                            .maxSelectable(1)
//                            .cropStyle(CropImageView.Style.CIRCLE)
//                            .isCropSaveRectangle(false)
//                            .gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
//                            .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
//                            .originalEnable(true)
//                            .maxOriginalSize(10)
//                            .thumbnailScale(0.85f)
//                            .imageEngine(new GlideEngine())
//                            .setOnSelectedListener(new OnSelectedListener() {
//                                @Override
//                                public void onSelected(@NonNull List<Uri> uriList, @NonNull List<String> pathList) {
//                                    if (uriList != null && uriList.size() > 0 && pathList != null && pathList.size() > 0) {
//                                        Log.d("Leo", uriList.size() + " " + uriList.get(0) + " " + pathList.size() + " " + pathList.get(0));
//                                    }
//                                }
//                            })
//                            .forResult(1000);
                } else {
                    //只要有一个权限被拒绝，就会执行
                    Toast.makeText(zyjActivity.this, "未授权权限，部分功能不能使用", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void uploadOssImage(OssService ossService, int position, int sumNumber, String loacalImageurl) {
//开始上传，参数分别为content，上传的文件名filename，上传的文件路径filePath
        ossService.beginupload(zyjActivity.this, position, sumNumber, createOssurl(), loacalImageurl, new OssService.OnUploadListener() {
            @Override
            public void onProgress(double progress) {

            }

            @Override
            public void onSuccess(int position, int sumNumber, String uploadPath, String imageUrl) {
                Log.d("Debug", "图片上传成功回调" + "上传地址" + uploadPath);
                Log.d("Debug", "图片上传成功回调" + "阿里云地址" + imageUrl);
                //上传头像
                identify(imageUrl);
            }

            @Override
            public void onFailure(int position) {

            }
        });
    }

    /**
     * 创建oss上传地址
     *
     * @return
     */
    public String createOssurl() {
        String userid = PrefShare.getInstance(zyjActivity.this).getString("userId");
        String random = (int) ((Math.random() * 9 + 1) * 1000) + "";
        String url = "sbid/" + getTime() + "/" + userid + System.currentTimeMillis() + random + "ad";
        return url;
    }

    public String getTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date(System.currentTimeMillis());
        String time = sdf.format(date);
        return time;
    }


    //上传的进度回调
    String accessKeyId = "LTAI4FpBbo7yfZx1waLnveHQ";
    String accessKeySecret = "aymEw4VmeHKuFtnyYVoyDIziQrCLff";
    String endpoint = "http://cdnzkds.zyfjsm.com";
    String bucketName = "gpos-zkds";
    Dialog gloablDialog;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d("Debug", "到达回调这里");
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1000:
                    try {
                        gloablDialog = StyledDialog.buildLoading(zyjActivity.this, "识别中...").show();
                        //没压缩之前的文件
                        List<String> originalPath = Matisse.obtainPathResult(data);
//                    //获得没压缩的文件
                        File file = FileUtil.getFileByPath(Matisse.obtainPathResult(data).get(0));
                        //下面是上传阿里云
                        long fileSize = getFileSize(file);
                        Log.d("Debug", "返回的图片数据大小" + fileSize / 1024 + "kb");
                        String url = "";
                        //图片大于大于200kb  压缩操作
                        if (fileSize / 1024 > 200) {
                            //这个是下面Compressor 失败之后备用的上传图片地址
                            String CompressorFailpath = "";
                            if (originalPath.size() == 1) {
                                CompressorFailpath = BitmapUtil.compressImage(file.getPath());
                            }
                            //大于1m  采样率压缩
                            if (fileSize / 1024 / 1024 > 1) {
                                file = BitmapUtil.compressImageReturnFile(file.getPath(), 100, 2);
                            }
                            Log.d("Debug", "另外一种压缩方式的地址为------------" + getFileSize(file) / 1024);
                            try {
                                File compressedImage = new Compressor(zyjActivity.this)
                                        .setMaxWidth(640)
                                        .setMaxHeight(480)
                                        .setQuality(90)
                                        .setCompressFormat(Bitmap.CompressFormat.JPEG)
                                        .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
                                                Environment.DIRECTORY_PICTURES).getAbsolutePath())
                                        .compressToFile(file);
                                Log.d("Debug", "另外一种压缩方式的地址为" + compressedImage.getPath());
                                url = compressedImage.getPath();
                            } catch (Exception e) {
                                //部分手机Compressor 不成功 用上面的CompressorFailpath备用的地址
                                url = CompressorFailpath;
                            }
                        } else {
                            url = file.getPath();
                        }

                        //初始化OssService类，参数分别是Content，accessKeyId，accessKeySecret，endpoint，bucketName（后4个参数是您自己阿里云Oss中参数）
                        OssService ossService = new OssService(zyjActivity.this, accessKeyId, accessKeySecret, endpoint, bucketName);
                        //初始化OSSClient
                        ossService.initOSSClient();
                        uploadOssImage(ossService, 1, 1, url);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //上传图片操作
                    break;
                case 888:
                    //扫二维码操作
                        if (data == null || data.getExtras() == null) {
                            return;
                        }
                        String url = data.getStringExtra("url");
                        Log.d("Debug","扫一扫界面返回的url"+url);
                        if (!TextUtils.isEmpty(url)) {
                            codeidentify(url);
                        }else {
                            ToastUtil.showShort("二维码识别失败");
                        }
                    break;
            }

        }
    }


    /**
     * 获取指定文件大小
     */
    public long getFileSize(File file) throws Exception {
        long size = 0;
        if (file.exists()) {
            FileInputStream fis = null;
            fis = new FileInputStream(file);
            size = fis.available();
        } else {
            Log.e("获取文件大小", "文件不存在!");
        }
        return size;
    }


}
